# -*- coding: utf-8 -*-

from django import forms
from django.contrib.auth import authenticate, login, logout
from django.contrib.auth.models import User

class UserLoginForm(forms.Form):
    username = forms.CharField(max_length=30)
    password = forms.CharField(max_length=30, widget=forms.PasswordInput)
    remember = forms.BooleanField(label="Remember me?", required=False)
    
    def __init__(self, request, *args, **kwargs):
        self.request = request
        super(UserLoginForm, self).__init__(*args, **kwargs)
        
    def clean(self):
        """Logs in the user if the form data provided is valid"""
        username = self.cleaned_data['username']
        password = self.cleaned_data['password']
        remember = self.cleaned_data.get('remember')
        
        if username and password:
            self.user = authenticate(username=username, password=password)
            if self.user is not None:
                if self.user.is_active:
                    login(self.request, self.user)
                    
                    if not remember:
                        self.request.session.set_expiry(None)
                else:
                    raise forms.ValidationError, "Your account is not active."
            else:
                raise forms.ValidationError, "Invalid username/password combination."
        return self.cleaned_data
class RegisterFormUser(forms.Form):
    username = forms.CharField(max_length=30, help_text="Maximum of 30 characters.")
    
    password = forms.CharField(max_length=20, widget=forms.PasswordInput, label="Password")
    password2 = forms.CharField(max_length=20, help_text="Enter the same password as above.", 
        widget=forms.PasswordInput, label="Password (again)")
    
    email = forms.EmailField(help_text="Must be a valid email address.", label="Email Address")
    email2 = forms.EmailField(help_text="Re-enter the same email address as above", label="Email Address (again)")
    
    def clean_username(self):
        """Verifies that the username given is not already taken"""
        username = self.cleaned_data['username']
        
        try:
            User.objects.get(username=username)
        except User.DoesNotExist:
            return username
        else:
            raise forms.ValidationError, "That username is already taken."
    def clean_password2(self):
        """Verifies that both passwords are the same."""
        password = self.cleaned_data['password']
        password2 = self.cleaned_data['password2']
        
        if password != password2:
            raise forms.ValidationError, "Passwords don't match."
        
        return password2
    
    def clean_email2(self):
        """Verifies that both email addresses are the same."""
        
        email = self.cleaned_data['email']
        email2 = self.cleaned_data['email2']
        
        if email != email2:
            raise forms.ValidationError, "Email addresses don't match."
        
        return email2
        
class RegisterFormProfile(forms.Form):
    bio = forms.CharField(max_length=400, help_text="A short bio about yourself...max of 400 characters", 
        required=False, widget=forms.Textarea)
    twitter = forms.CharField(max_length=30, help_text="Your Twitter username, if you have one.", required=False)